﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Client_Revenue
{
    public partial class ContractSalesInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                using (var DE = new dominiumEntities())
                {
                    var clients = (from c  in DE.Clients 
                                                join o in DE.Contracts on c.ClientId equals o.ClientId
                                          select new
                                           {
                                               c.ClientId,
                                               c.ClientName
                                           }).ToList();

                    ClientsGridView.DataSource = clients;
                    ClientsGridView.DataBind();

                }
            }
        }

        protected void ClientsGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                using (var DE = new dominiumEntities())
                {
                    int iClientid =  (Convert.ToInt32(ClientsGridView.SelectedValue));
                   
                    var theSalesDetails = (from p in DE.People
                                           join sd in DE.SalesInfoes on p.PersonId equals sd.PersonId
                                           join r in DE.SalesRoles on sd.SalesRoleId equals r.SalesRoleId
                                           join c in DE.Contracts on sd.ContractId equals c.ContractId
                                           where c.ClientId == iClientid
                                           orderby  p.LastName
                                           select new
                                           {
                                             FullName =   p.FirstName + " " + p.LastName,
                                               r.RoleName,
                                               sd.Commission,
                                               sd.Fee,
                                           }).Distinct().ToList();

                    gvContractsSalesDetails.DataSource = theSalesDetails; 
                    gvContractsSalesDetails.DataBind();
                }
            }
            catch (Exception ex)
            { throw ex; }
        }

        protected void gvContractsSalesDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gvContractsSalesDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvContractsSalesDetails.SelectedIndex = -1;
        }

        protected void gvContractsSalesDetails_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void gvContractsSalesDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void ClientsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gvContractsSalesDetails_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void gvContractsSalesDetails_Sorting(object sender, GridViewSortEventArgs e)
        {

        }
    }
}